from django.shortcuts import render, redirect
from django.http import JsonResponse, HttpResponse
from app.models import EmployeeUser, client, table, order, dishes, ingredients, dish_ingre, myLog
from django.contrib.auth import authenticate, login, logout


# Create your views here.


def myLog_decrator(request, operatType):
    opid = EmployeeUser.objects.get(id=request.session.get('_auth_user_id'))
    myLog.objects.create(operatorId=opid, text=operatType)
    print("日志写入成功"+operatType)
    pass


def ajax_checkname(request):
    resule = {}
    text = request.POST.get('text')
    res = EmployeeUser.objects.filter(username=text)
    if len(res) == 0:
        resule['resule'] = "可用"
        pass
    else:
        resule['resule'] = ""
        pass
    return JsonResponse(resule)
    pass


def mylogin(request):
    if request.POST:
        user = request.POST.get('user')
        pwd = request.POST.get('pwd')
        obj = authenticate(username=user, password=pwd)
        rem = request.POST.get('rem')
        if obj:
            # 验证通过，设置session
            login(request, obj)
            # 写入日志
            myLog_decrator(request, "雇员{0}登录".format(user))
            if rem:
                request.session.set_expiry(None)
                pass
            else:
                request.session.set_expiry(0)
                pass
            if obj.job == "老板":
                return redirect("/showtable/")
            if obj.job == "后厨":
                return redirect("/kitchenHome/")
            if obj.job == "服务生":
                return redirect("/waiter/")
            if obj.job == "前台":
                return redirect("/reception/")
            pass
        else:
            return render(request, 'login.html', {'msg': '账号密码错误'})
            pass
        pass
    else:
        return render(request, 'login.html')
    pass


def index(request):
    return redirect("/login/")
    pass


def register(request):
    if request.POST:
        datadict = {"0": "后厨",
                    "1": "前台",
                    "2": "服务生",
                    "3": "老板",
                    }
        username = request.POST.get('username')
        password = request.POST.get('pwd')
        name = request.POST.get('name')
        phone = request.POST.get('phone')
        job = datadict[request.POST.get('job')]
        EmployeeUser.objects.create_user(username=username, password=password, name=name, phone=phone, job=job)
        return render(request, 'register.html', {"msg": "注册成功"})
        pass
    else:
        return render(request, 'register.html')
    pass


def mylogout(request):
    logout(request)
    return redirect("/login/")


# 测试函数，用来填充数据库中的表
def text(requset):
    # # 创建用户
    # client_list = [
    #     {"level": "会员", "allCost": 100, "name": "王老二", "sex": "女", "age": "30", "phone": "18612345345",
    #      "idCard": "411111111111111111"},
    #     {"level": "会员", "allCost": 10023, "name": "老二", "sex": "男", "age": "50", "phone": "18612344444",
    #      "idCard": "411111146372837111"},
    #     {"level": "会员", "allCost": 1020, "name": "啊丽丽", "sex": "女", "age": "20", "phone": "18612344585",
    #      "idCard": "411138276411111111"},
    #     {"level": "会员", "allCost": 1000, "name": "老八", "sex": "男", "age": "14", "phone": "18628282828",
    #      "idCard": "422233444453456323"}
    # ]
    # for dic in client_list:
    #     client.objects.create(level=dic['level'],allCost=dic['allCost'],name=dic['name'],sex=dic['sex'],age=dic['age'],phone=dic['phone'],idCard=dic['idCard'])
    #     # 使用默认值创建用户
    #     client.objects.create()
    #     pass
    # # 使用默认值创建餐桌
    # for i in range(5,10):
    #     table.objects.create(number=i)
    #     pass
    # # 创建菜品数据
    # name=['宫保鸡丁','面包','老八秘制小汉堡','青椒肉丝','大肠刺身']
    # for i in range(5):
    #     dishes.objects.create(name=name[i], price=i)
    # # 创建食材数据
    # name=['美汁汁','腐乳','老干妈','鲜大肠','香菜']
    # for i in range(5):
    #     ingredients.objects.create(name=name[i], num=i)
    # # 创建订餐数据
    # for i in range(2,6):
    #     clientid = client.objects.get(id=i)
    #     dishesid = dishes.objects.get(id=i)
    #     operatorid = EmployeeUser.objects.get(id=i)
    #     tableid = table.objects.get(id=i)
    #     order.objects.create(clientid=clientid,dishesid=dishesid,operatorid=operatorid,tableid=tableid)
    # #  创建菜单-食材表
    # for i in range(1,6):
    #     DI_ingr = ingredients.objects.get(id=i)
    #     DI_dish = dishes.objects.get(id=i+1)
    #     DI_Num = i
    #     dish_ingre.objects.create(DI_ingr=DI_ingr,DI_dish=DI_dish,DI_Num=DI_Num)
    return redirect('/index/')
    pass
